/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.util.Observable;
import java.util.Observer;
import javax.swing.*;


import messageprocessors.clients.*;


/**
 *
 * @author Shanks
 */
public class BaseClientGUI extends JFrame implements Observer{
    
	private JTextArea tarea;
	private JScrollPane scrollpane;

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel panel;    
	private int messageCounter;

    
    public BaseClientGUI() {
        super("GUI");     
    }
    
    
    public void initGUI(BaseClient o) {
    	
        setSize(300,300);
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setTitle("Appserver Status");

        panel = new JPanel();
      
        o.addObserver(this);
        
        panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
		
		tarea = new JTextArea();
		tarea.setText("Message will be displayed here!");
		
		
		scrollpane = new JScrollPane(tarea);
		panel.add(scrollpane);
		
		
		getContentPane().add(panel);
        
        setVisible(true);
         	
    }
    
    
    
    @Override
    public void update(Observable o, Object arg) {
    	
        if (o instanceof AppserverClient){
        	messageCounter++;
    		
        	AppserverClient ap = (AppserverClient)o;
        	String newText = tarea.getText() + "\n\n" + messageCounter + ") \n Last Message Received: \n" + ap.getLastMessageReceived();
    		newText += "\n Last Message Sent: \n" + ap.getLastMessageSent();
    	
//        	ap.setAusgabe(ap.getLastMessageReceived()+"\n");
//        	ap.setAusgabe(ap.getLastMessageSent()+"\n");
//        	if (ap.getLastArt()!=null){
//        	ap.setAusgabe(ap.getLastArt().toString()+"\n");		
//        	}
//        	setTitle("Appserver");
//            if ((ap.getAusgabe()!=null)||(ap.getAusgabe().trim()!="")){
//                tarea.setText(ap.getAusgabe());
//            }
            if ((newText!=null)||(newText.trim()!="")){
                tarea.setText(newText);
            }
                       
        }        
    }
    
    public void showPopup(String text) {
    	JOptionPane.showMessageDialog(this, text);
    }

    
}	